<?php

namespace app\index\controller;

use think\Controller;

class Order extends Base {
	public function index() {
		if ($this->request->isPost ()) {
			$money = $this->request->param ( 'money' );
			$info = model ( 'Member' )->getInfo ( array ([ 'user_id','=',$this->uid ] ) );
			$user = model ( 'Member' )->getInfo ( array ([ 'user_name','=',$this->request->param ( 'username' ) ] ) );
			if ($this->member ['freeze'] == 1)
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'freeze_msg' ) ) );
			elseif ($money <= 0 || $money > $info ['money'])
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'insufficient_balance' ) ) );
			elseif (count ( $user ) > 0 && $user ['user_id'] != $this->uid) {
				$recuser = model ( 'Member' )->getInfo ( array ([ 'm.user_id','=',$user ['user_id'] ] ) );
				if ($recuser ['status'] == 0) {
					echo json_encode ( array ('code' => 0,'msg' => lang ( 'receive_account_abnormal' ) ) );
					exit ();
				}
				if ($user ['role_id'] > 0) {
					$role = model ( 'Member' )->roleInfo ( array ([ 'id','=',$user ['role_id'] ] ) );
					// 当天额度
					$map [] = [ 'receive_uid','=',$user ['user_id'] ];
					$map [] = [ 'create_time','>=',strtotime ( date ( 'Y-m-d 00:00:00' ) ) ];
					$map [] = [ 'create_time','<=',strtotime ( date ( 'Y-m-d 23:59:59' ) ) ];
					$num = model ( 'Order' )->getMoney ( $map );
					if (($num + $money) > $role ['daily_pay']) {
						echo json_encode ( array ('code' => 0,'msg' => lang ( 'daily_receive_limit' ) ) );
						exit ();
					}
					unset ( $map );
					// 单月额度
					$t = date ( "t", strtotime ( date ( 'Y-m-d' ) ) );
					$map [] = [ 'receive_uid','=',$user ['user_id'] ];
					$map [] = [ 'create_time','>=',strtotime ( date ( "Y-m-01 00:00:00" ) ) ];
					$map [] = [ 'create_time','<=',strtotime ( date ( "Y-m-$t 23:59:59" ) ) + 24 * 60 * 60 * 100 ];
					$num = model ( 'Order' )->getMoney ( $map );
					if (($num + $money) > $role ['month_pay']) {
						echo json_encode ( array ('code' => 0,'msg' => lang ( 'monthly_receive_limit' ) ) );
						exit ();
					}
					$data ['user_id'] = $this->uid;
					$data ['money'] = $money;
					$data ['status'] = 1;
					$data ['receive_uid'] = $user ['user_id'];
					$data ['create_time'] = time ();
					$data ['transfer_fee'] = $money * $this->website ['transfer_fee'] / 100;
					$log = model ( 'Order' )->add ( $data );
					if ($log > 0) {
						model ( 'Member' )->incMoney ( array ([ 'user_id','=',$user ['user_id'] ] ), $money - $money * $this->website ['transfer_fee'] / 100 );
						// if ($info ['isinside'] == 0) {
						model ( 'Member' )->decMoney ( array ([ 'user_id','=',$this->uid ] ), $money );
						// }
						echo json_encode ( array ('code' => 99,'msg' => lang ( 'success' ) ) );
					} else
						echo json_encode ( array ('code' => 0,'msg' => lang ( 'fail' ) ) );
				} else
					echo json_encode ( array ('code' => 0,'msg' => lang ( 'lest_vip1' ) ) );
			} else
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'receive_account_error' ) ) );
		} else {
			$data = model ( 'Order' )->getlist ( array ([ 't.user_id','=',$this->uid ] ) );
			$this->assign ( 'data', $data );
			$this->assign ( 'fee', $this->website ['transfer_fee'] / 100 );
			return $this->fetch ();
		}
	}
	function recode() {
		$data = model ( 'Order' )->getlist ( array ([ 't.user_id|t.receive_uid','=',$this->uid ] ), 100 );
		$list = $data->items ();
		foreach ( $list as $k => $v ) {
			if ($v ['user_id'] == $this->uid)
				$list [$k] ['type'] = lang ( 'transfer' );
			else
				$list [$k] ['type'] = lang ( 'receive' );
			if ($v ['receive_uid'] == $this->uid)
				$list [$k] ['money'] = $v ['money'] - $v ['transfer_fee'];
		}
		$this->assign ( 'last', $data->items () [0] );
		$this->assign ( 'list', $list );
		$this->assign ( 'data', $data );
		return $this->fetch ();
	}
}
